// pages/bill-detail/bill-detail.js

const requestPutBillToClosed = require('../../utils/request.js').requestPutBillToClosed
const showDialog = require('../../utils/util.js').showDialog
const successBackPage = require('../../utils/wx-navigate.js').successBackPage
//获取应用实例
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    bill:{},

    isToCloseBill:false,
    moneydaySelect: { label: "收租日期", placeholder: "请选择收租日期", mode: "date", value: '' },

    moneywaySelect: {
      label: "收租渠道", placeholder: "请选择收租渠道", mode: "selector",
      range: ['支付宝', '微信',"银联", '现金', '其他'], index: ''
    },
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let data = JSON.parse(options.data)
    this.setData({
      bill: data
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  bindtoclosebill:function(){
    this.setData({
      isToCloseBill: !this.data.isToCloseBill
    })
  },
  bindchange: function (e) {
    let select
    switch (e.currentTarget.dataset.id) {
      // 起租日期
      case this.data.moneydaySelect.label:
        select = this.data.moneydaySelect
        select.value = e.detail.value
        this.setData({
          moneydaySelect: select
        })
        console.log("收租日期")
        break
      // 租期时长
      case this.data.moneywaySelect.label:
        select = this.data.moneywaySelect
        select.index = e.detail.value
        this.setData({
          moneywaySelect: select
        })

        console.log("收租渠道")
        break
    }
  },
  bindbtntap:function(){
    console.log(this.data.bill)
    console.log(this.data.moneydaySelect.value)
    console.log(this.data.moneywaySelect.range[this.data.moneywaySelect.index])
    showDialog("确认保存","保存之后将不可修改，请确保你已收到该笔房租",()=>{
      requestPutBillToClosed(this.data.bill._id, this.data.moneydaySelect.value, this.data.moneywaySelect.range[this.data.moneywaySelect.index],()=>{

        app.updateIndexPageData()
        successBackPage('关闭账单成功')
        
      })
    })
    
    
  }
})